package com.example.jinzproduct.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.example.commons.entity.*;
import com.example.commons.utils.*;
import com.example.jinzproduct.server.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/jinZhiIndex/jzApi/sheet")
public class SheetController {

    @Autowired
    private ExaminationAnswerSheetService examinationAnswerSheetService;
    @Autowired
    private DictService dictService;
    @Autowired
    private StuSignUpService stuSignUpService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationPaperService examinationPaperService;
    @Autowired
    private ExaminationQuestionsService examinationQuestionsService;
    @Autowired
    private LogService logService;
    @Autowired
    private ExceptionAddtimeService exceptionAddtimeService;
    @Autowired
    private CertificationBatchService certificationBatchService;
    @Autowired
    private TestPaperTemplateService testPaperTemplateService;
    @Autowired
    private TemplateDetailsService templateDetailsService;
    @Autowired
    private ItemBankService itemBankService;

    @Autowired
    private ExaminationAnswerSheetPracticeService examinationAnswerSheetPracticeService;

    /**
     * 考试查看详情
     * @param id
     * @return
     */
    @PostMapping("/info")
  public   Object infoPractice(Long id) {
        if (id==null){
            return ApiResult.fail("参数不能为空！");
        }
        ExaminationAnswerSheetDO examinationAnswerSheet = examinationAnswerSheetService.getById(id);


//        if(examinationAnswerSheet.getExamType()==2){
//            return ApiResult.fail("无法查看详情！");
//        }

        if (examinationAnswerSheet.getEpaperId() != null) {
            ExaminationPaperDO examinationPaperDO = examinationPaperService.getById(examinationAnswerSheet.getEpaperId());
            if (examinationPaperDO != null) {
                if(examinationPaperDO.getEbatchId()!=null){
                    ExaminationBatchDO examinationBatchDO= examinationBatchService.getById(examinationPaperDO.getEbatchId());
                    examinationAnswerSheet.setBatchName(examinationBatchDO.getName());
                }
                examinationAnswerSheet.setEpaperName(examinationPaperDO.getName());
            }
        }

        if (examinationAnswerSheet.getStuSignId() != null) {
            StuSignUpDO stuSignUpDO = stuSignUpService.getById(examinationAnswerSheet.getStuSignId());
            if (stuSignUpDO != null) {
                examinationAnswerSheet.setStuName(stuSignUpDO.getName());
            }
        }
        List<Map> maps = new ArrayList<>();
        Integer results = 0;
        Integer error = 0;
        Integer not=0;
        String[] ssr = examinationAnswerSheet.getAnswerList().split("\\{\"id\":\"");
        for (int i = 1; i < ssr.length; i++) {
            Map map = new HashMap();
            String[] ssc = ssr[i].split("\"}");
            for (int j = 0; j < ssc.length; j++) {
                String[] ssa = ssc[j].split("\",\"value\":\"");
                if (ssa.length > 1) {
                    ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.getById(ssa[0]);
                    map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                    map.put("introduce",examinationQuestionsDO.getIntroduce());//题目内容
                    map.put("type",examinationQuestionsDO.getType());//题目内容
                    map.put("topicAnalysis","无");//题目解析
                    map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                    if (examinationQuestionsDO.getType() == 0 || examinationQuestionsDO.getType() == 2) {//单选题和判断题，答案唯一，可以不用拆解了
                        if (examinationQuestionsDO.getAnswers().equals(ssa[1])) {
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",true);
                           // System.out.println("正确单卷ID："+examinationQuestionsDO.getId());
                            results = results + examinationQuestionsDO.getScore();
                        }else{
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",false);
                            error++;
                        }
                    } else if (examinationQuestionsDO.getType() == 1) {//多选题因为答案有多个，需要循环判断一下是否包含该答案

                        boolean isRight=true;
                        if(ssa[1]==null||ssa[1]==""||ssa[1].length()==1){
                            isRight=false;
                        }else{
                            String[] ssb = ssa[1].substring(0,ssa[1].length()-2).split(";;");
                            String answer = examinationQuestionsDO.getAnswers();
                            if(ssb.length!=answer.split(";;").length){
                                isRight=false;
                            }else {
                                for (int n = 0; n < ssb.length; n++) {
                                    //if (ssb[n].equals("A") || ssb[n].equals("B") || ssb[n].equals("C") || ssb[n].equals("D") || ssb[n].equals("E") || ssb[n].equals("F")) {
                                    if (!answer.contains(ssb[n])) {
                                        isRight = false;
                                        break;
                                    }

                                }
                            }
                        }

                        if(isRight){
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",true);
                           // System.out.println("正确单卷ID："+examinationQuestionsDO.getId());
                            results = results + examinationQuestionsDO.getScore();
                        }else{
                            error++;
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",false);
                        }
                    }
                    maps.add(map);
                }else if(ssa.length == 1 && !ssa[0].equals(",") && !ssa[0].equals("]")){
                    ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.getById(ssa[0]);
                    map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                    map.put("type",examinationQuestionsDO.getType());//题号
                    map.put("introduce",examinationQuestionsDO.getIntroduce());//题目内容
                    map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                    map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                    map.put("options","");//考生选项
                    map.put("yesOrNo",false);
                    not++;
                    maps.add(map);
                }
            }
        }
        examinationAnswerSheet.setAnswerLists(maps);
        //分数
        examinationAnswerSheet.setScore(results);
        examinationAnswerSheet.setError(error);
        examinationAnswerSheet.setSuccess(maps.size()-(error+not));
        examinationAnswerSheet.setNotScored(not);

        //examinationAnswerSheet.setCreate_time();

        return   ApiResult.success(examinationAnswerSheet);
    }




    /**
     * 练习刷题查看列表
     * type 0 练习  1考试
     * @param
     * @return
     */
    @PostMapping("/userQuestionsInfo")
    Object userQuestionsInfo(String mobile,Integer type) {

        if(type==0){
            QueryWrapper<ExaminationAnswerPracticeSheetDO >wrapper=new QueryWrapper<>();
            wrapper.eq("userid",JWTUtil.getUser().getId());
            wrapper.orderByDesc("id");
            List<ExaminationAnswerPracticeSheetDO> list=   examinationAnswerSheetPracticeService.list(wrapper);

            for(ExaminationAnswerPracticeSheetDO li:list){
                String prifile=li.getEpaperId().toString();
                /**
                 *                 case "0":cn_id = "1241246900989841410";break;
                 *                 case "1":cn_id = "1241246701718458370";break;
                 *                 case "2":cn_id = "1241245991509544961";break;
                 *                    case "2":cn_id = "1522773090287075329   ";  1522773009475420162 break;// 托育
                 */
                switch (prifile){
                    case "1241245212023312386": li.setProfileName("幼儿照护模拟考试");
                        li.setPicType(1);
                    break;
                    case "1375327865662226434": li.setProfileName("产后恢复模拟考试");
                        li.setPicType(2);
                        break;
                    case "1522773009475420162": li.setProfileName("长沙市托育");
                        li.setPicType(3);
                        break;
                    case "1522773009475485497": li.setProfileName("河南省托育职业技能模拟考试");
                        li.setPicType(4);
                        break;
                }

                String tmp = StringEscapeUtils.unescapeJavaScript(li.getAnswerList());
             // int score=  sumScore(li.getAnswerList());
               // li.setScore(score);
                li.setAnswerList(tmp);
            }
            return ApiResult.success(list);
        }else {

            QueryWrapper<ExaminationAnswerSheetDO >wrapper=new QueryWrapper<>();
            wrapper.eq("userid",JWTUtil.getUser().getId());
            wrapper.orderByDesc("id");
            List<ExaminationAnswerSheetDO> list=   examinationAnswerSheetService.list(wrapper);

            for(ExaminationAnswerSheetDO li:list){
                ExaminationPaperDO examinationPaperDO = examinationPaperService.getById(li.getEpaperId());

                if(examinationPaperDO.getName().contains("幼儿照护")){
                    li.setPicType(1);
                }else if(examinationPaperDO.getName().contains("产后恢复")){
                    li.setPicType(1);
                }
                else if(examinationPaperDO.getName().contains("长沙市托育")){
                    li.setPicType(3);
                }
                else if(examinationPaperDO.getName().contains("河南托育")) {
                    li.setPicType(4);
                } else if(examinationPaperDO.getName().contains("泰康")){
                    li.setPicType(5);
                  } else if(examinationPaperDO.getName().contains("考务技术员及督导员")) {
                    li.setPicType(6);
                }
//                String prifile=li.getEpaperId().toString();
//
//                switch (prifile){
//                    case "1241245212023312386": li.setProfileName("幼儿照护模拟考试");
//                        li.setPicType(1);
//                        break;
//                    case "1375327865662226434": li.setProfileName("产后恢复模拟考试");
//                        li.setPicType(2);
//                        break;
//
//                }

                if(examinationPaperDO!=null){
                    li.setProfileName(examinationPaperDO.getName());
                }else {
                    li.setProfileName("1+X证书考试");
                }
                li.setProfileName(li.getProName());
                String tmp = StringEscapeUtils.unescapeJavaScript(li.getAnswerList());
                li.setAnswerList(tmp);

                int score=  sumScore1(li.getAnswerList());
                li.setScore(score);
            }
            return ApiResult.success(list);
        }
    }

    public Integer sumScore1( String examinationAnswerSheet){
        List<Map> maps = new ArrayList<>();
        Integer results = 0;
        Integer error = 0;
        Integer not = 0;
        String[] ssr = examinationAnswerSheet.split("\\{\"id\":\"");
        for (int i = 1; i < ssr.length; i++) {
            Map map = new HashMap();
            String[] ssc = ssr[i].split("\"}");
            for (int j = 0; j < ssc.length; j++) {
                String[] ssa = ssc[j].split("\",\"value\":\"");
                if (ssa.length > 1) {
                    ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.getById(ssa[0]);

                    map.put("options",ssa[1]);//考生选项
                   // map.put("topicAnalysis",examinationQuestionsDO.getTopicAnalysis());//题目解析
                   // map.put("type",examinationQuestionsDO.getTopicTypes());

                    // map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                   // map.put("introduce",examinationQuestionsDO.getTopicContent());//题目内容
                    if(examinationQuestionsDO!=null){

                    map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                    if (examinationQuestionsDO.getType() == 0 || examinationQuestionsDO.getType() == 2) {//单选题和判断题，答案唯一，可以不用拆解了
                        if (examinationQuestionsDO.getAnswers().equals(ssa[1])) {
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项

                            map.put("yesOrNo",true);
                          //  System.out.println("正确单卷ID："+examinationQuestionsDO.getId());
                            results = results + examinationQuestionsDO.getScore();
                        }else{
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",false);
                            error++;
                        }
                    } else if (examinationQuestionsDO.getType() == 1) {//多选题因为答案有多个，需要循环判断一下是否包含该答案

                        boolean isRight=true;
                        if(ssa[1]==null||ssa[1]==""){
                            isRight=false;
                        }else{
                            String[] ssb = ssa[1].substring(0,ssa[1].length()).split(";;");
                            String answer = examinationQuestionsDO.getAnswers();
                            if(ssb.length!=answer.split(";;").length){
                                isRight=false;
                            }else {
                                for (int n = 0; n < ssb.length; n++) {
                                    //if (ssb[n].equals("A") || ssb[n].equals("B") || ssb[n].equals("C") || ssb[n].equals("D") || ssb[n].equals("E") || ssb[n].equals("F")) {
                                    if (!answer.contains(ssb[n])) {
                                        isRight = false;
                                        break;
                                    }

                                }
                            }
                        }
                        if(isRight){
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",true);
                            results = results +  examinationQuestionsDO.getScore();
                        }else{
                            error++;
                            map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",false);
                        }
                    }

                    }else {
                       log.info("未找到试卷表中对应信息："+ssa[0]+"----ssr=="+ssr);
                    }
                    maps.add(map);

                }else if(ssa.length == 1 && !ssa[0].equals(",") && !ssa[0].equals("]")){
                    ItemBankDO examinationQuestionsDO = itemBankService.getById(ssa[0]);
                    if(examinationQuestionsDO!=null){
                        // map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                        map.put("introduce",examinationQuestionsDO.getTopicContent());//题目内容
                        map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                        map.put("answers",examinationQuestionsDO.getRightKey());//正确答案
                        map.put("options","");//考生选项
                        map.put("type",examinationQuestionsDO.getTopicTypes());
                        map.put("yesOrNo",false);
                        //error++;
                        maps.add(map);
                    }
                    not++;

                }
            }
        }
        return results;
    }

    /**
     * 练习刷题查看刷题详情
     * @param id
     * @return
     */
    @PostMapping("/infoPractice")
    Object info(Long id) {
        ExaminationAnswerPracticeSheetDO examinationAnswerSheet = examinationAnswerSheetPracticeService.getById(id);
        ExaminationAnswerPracticeSheetDO examinationAnswerSheet1=examinationAnswerSheet;

        List<Map> maps = new ArrayList<>();
        Integer results = 0;
        Integer error = 0;
        Integer not = 0;
        String[] ssr = examinationAnswerSheet.getAnswerList().split("\\{\"id\":\"");
        for (int i = 1; i < ssr.length; i++) {
            Map map = new HashMap();
            String[] ssc = ssr[i].split("\"}");
            for (int j = 0; j < ssc.length; j++) {
                String[] ssa = ssc[j].split("\",\"value\":\"");
                if (ssa.length > 1) {
                    ItemBankDO examinationQuestionsDO = itemBankService.getById(ssa[0]);
               //     System.out.println("type=="+examinationQuestionsDO.getType());
                    map.put("options",ssa[1]);//考生选项
                    map.put("topicAnalysis",examinationQuestionsDO.getTopicAnalysis());//题目解析
                    map.put("type",examinationQuestionsDO.getTopicTypes());

                   // map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                    map.put("introduce",examinationQuestionsDO.getTopicContent());//题目内容
                    map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                    if (examinationQuestionsDO.getTopicTypes() == 0 || examinationQuestionsDO.getTopicTypes() == 2) {//单选题和判断题，答案唯一，可以不用拆解了
                        if (examinationQuestionsDO.getRightKey().equals(ssa[1])) {
                            map.put("answers",examinationQuestionsDO.getRightKey());//正确答案
                            map.put("options",ssa[1]);//考生选项

                            map.put("yesOrNo",true);
                          //  System.out.println("正确单卷ID："+examinationQuestionsDO.getId());
                            results = results + 1;
                        }else{
                            map.put("answers",examinationQuestionsDO.getRightKey());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",false);
                            error++;
                        }
                    } else if (examinationQuestionsDO.getTopicTypes() == 1) {//多选题因为答案有多个，需要循环判断一下是否包含该答案

                        boolean isRight=true;
                        if(ssa[1]==null||ssa[1]==""){
                            isRight=false;
                        }else{
                            String[] ssb = ssa[1].substring(0,ssa[1].length()-2).split(";;");
                            String answer = examinationQuestionsDO.getRightKey();
                            if(ssb.length!=answer.split(";;").length){
                                isRight=false;
                            }else {
                                for (int n = 0; n < ssb.length; n++) {
                                    //if (ssb[n].equals("A") || ssb[n].equals("B") || ssb[n].equals("C") || ssb[n].equals("D") || ssb[n].equals("E") || ssb[n].equals("F")) {
                                    if (!answer.contains(ssb[n])) {
                                        isRight = false;
                                        break;
                                    }

                                }
                            }
                        }
                        if(isRight){
                            map.put("answers",examinationQuestionsDO.getRightKey());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",true);
                            results = results + 1;
                        }else{
                            error++;
                            map.put("answers",examinationQuestionsDO.getRightKey());//正确答案
                            map.put("options",ssa[1]);//考生选项
                            map.put("yesOrNo",false);
                        }
                    }
                    maps.add(map);
                }else if(ssa.length == 1 && !ssa[0].equals(",") && !ssa[0].equals("]")){
                    ItemBankDO examinationQuestionsDO = itemBankService.getById(ssa[0]);

                   // map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                    map.put("introduce",examinationQuestionsDO.getTopicContent());//题目内容
                    map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                    map.put("answers",examinationQuestionsDO.getRightKey());//正确答案
                    map.put("options","");//考生选项
                    map.put("type",examinationQuestionsDO.getTopicTypes());
                    map.put("yesOrNo",false);
                    not++;
                    //error++;
                    maps.add(map);
                }
            }
        }
        examinationAnswerSheet.setAnswerLists(maps);
        //分数
        examinationAnswerSheet.setScore(results);
        examinationAnswerSheet.setError(error);
        examinationAnswerSheet.setSuccess(maps.size()-(error+not));
        examinationAnswerSheet.setNotScored(not);

        examinationAnswerSheet1.setScore(results);
        examinationAnswerSheet1.setError(error);
        examinationAnswerSheet1.setSuccess(maps.size()-(error+not));
        examinationAnswerSheet1.setNotScored(not);
    boolean b=    examinationAnswerSheetPracticeService.updateById(examinationAnswerSheet1);
       // log.info("答题日志修改="+b);
      return   ApiResult.success(examinationAnswerSheet);
    }


    //SELECT * from  jz_examination_answer_sheet WHERE epaper_id='1523847727477518338'
    @GetMapping("/infos1")
    Object infos( ) {
        QueryWrapper<ExaminationAnswerSheetDO> wrapper=new QueryWrapper<>();
        wrapper.eq("epaper_id","1523847727477518338");
        // wrapper.last("limit 20");
        List<Map> list1s=new ArrayList<>();
        List <ExaminationAnswerSheetDO> list=  examinationAnswerSheetService.list(wrapper);
        int i1=0;
        for(ExaminationAnswerSheetDO li:list){
            Long  id=li.getId();
            ExaminationAnswerSheetDO examinationAnswerSheet = examinationAnswerSheetService.getById(id);
            if (examinationAnswerSheet.getEpaperId() != null) {
                ExaminationPaperDO examinationPaperDO = examinationPaperService.getById(examinationAnswerSheet.getEpaperId());
                if (examinationPaperDO != null) {
                    examinationAnswerSheet.setEpaperName(examinationPaperDO.getName());
                }
            }
            if (examinationAnswerSheet.getStuSignId() != null) {
                StuSignUpDO stuSignUpDO = stuSignUpService.getById(examinationAnswerSheet.getStuSignId());
                if (stuSignUpDO != null) {
                    examinationAnswerSheet.setStuName(stuSignUpDO.getName());
                }
            }
            List<Map> maps = new ArrayList<>();
            Integer results = 0;
            String[] ssr = examinationAnswerSheet.getAnswerList().split("\\{\"id\":\"");

            for (int i = 1; i < ssr.length; i++) {
                Map map = new HashMap();
                Map hashMap=new HashMap();
                String[] ssc = ssr[i].split("\"}");
                for (int j = 0; j < ssc.length; j++) {
                    String[] ssa = ssc[j].split("\",\"value\":\"");
                    if (ssa.length > 1) {
                        ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.getById(ssa[0]);
                        map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                        map.put("introduce",examinationQuestionsDO.getIntroduce());//题目内容
                        map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                        if (examinationQuestionsDO.getType() == 0 || examinationQuestionsDO.getType() == 2) {//单选题和判断题，答案唯一，可以不用拆解了
                            if (examinationQuestionsDO.getAnswers().equals(ssa[1])) {
                                map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                                map.put("options",ssa[1]);//考生选项
                                map.put("yesOrNo",true);
                                hashMap.put("yesOrNo",true);
                                hashMap.put("num",examinationQuestionsDO.getTitleNumber());
                                hashMap.put("introduce",examinationQuestionsDO.getIntroduce());
                                results = results + examinationQuestionsDO.getScore();
                            }else{
                                map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                                map.put("options",ssa[1]);//考生选项
                                map.put("yesOrNo",false);
                                hashMap.put("yesOrNo",false);
                                hashMap.put("num",examinationQuestionsDO.getTitleNumber());
                                hashMap.put("introduce",examinationQuestionsDO.getIntroduce());
                            }
                        } else if (examinationQuestionsDO.getType() == 1) {//多选题因为答案有多个，需要循环判断一下是否包含该答案

                            boolean isRight=true;
                            if(ssa[1]==null||ssa[1]==""){
                                isRight=false;
                            }else{
                                String[] ssb = ssa[1].substring(0,ssa[1].length()-2).split(";;");
                                String answer = examinationQuestionsDO.getAnswers();
                                if(ssb.length!=answer.split(";;").length){
                                    isRight=false;
                                }else {
                                    for (int n = 0; n < ssb.length; n++) {
                                        //if (ssb[n].equals("A") || ssb[n].equals("B") || ssb[n].equals("C") || ssb[n].equals("D") || ssb[n].equals("E") || ssb[n].equals("F")) {
                                        if (!answer.contains(ssb[n])) {
                                            isRight = false;
                                            break;
                                        }

                                    }
                                }
                            }

                            if(isRight){
                                map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                                map.put("options",ssa[1]);//考生选项
                                map.put("yesOrNo",true);
                                hashMap.put("yesOrNo",true);
                                hashMap.put("introduce",examinationQuestionsDO.getIntroduce());
                                hashMap.put("num",examinationQuestionsDO.getTitleNumber());
                                results = results + examinationQuestionsDO.getScore();
                            }else{
                                map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                                map.put("options",ssa[1]);//考生选项
                                map.put("yesOrNo",false);
                                hashMap.put("yesOrNo",false);
                                hashMap.put("num",examinationQuestionsDO.getTitleNumber());
                                hashMap.put("introduce",examinationQuestionsDO.getIntroduce());
                            }
                        }
                        list1s.add(hashMap);
                        maps.add(map);
                    }else if(ssa.length == 1 && !ssa[0].equals(",") && !ssa[0].equals("]")){
                        ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.getById(ssa[0]);
                        map.put("titleNumber",examinationQuestionsDO.getTitleNumber());//题号
                        map.put("introduce",examinationQuestionsDO.getIntroduce());//题目内容
                        map.put("titleOptions",examinationQuestionsDO.getOptions());//题目选项
                        map.put("answers",examinationQuestionsDO.getAnswers());//正确答案
                        map.put("options","");//考生选项
                        map.put("yesOrNo",false);

                        maps.add(map);
                    }
                }

            }
            examinationAnswerSheet.setAnswerLists(maps);
            examinationAnswerSheet.setScore(results);
          //  System.out.println("执行中==============="+i1++);
        }
        List<Map> list1p=new ArrayList<>();

        for(Map mm:list1s){
            int number=0;
            Map map=new HashMap();
            boolean ss=false;
            for(Map mm1:list1p){
                if(mm.get("num").toString().equals(mm1.get("num").toString())){
                    ss=true;
                    number=Integer.valueOf( mm1.get("number").toString());
                    break;
                    // System.out.println("重复题目为："+mm.get("introduce"));
                }
            }
            if(ss==false){
                map.put("num",mm.get("num"));
                map.put("introduce",mm.get("introduce"));
                if(!mm.get("yesOrNo").equals(true)){
                    map.put("number",number+1);
                }else {
                    map.put("number",number);
                }
                list1p.add(map);
            }
        }

        for(Map mm:list1p){
            int number=0;
            for(Map mm1:list1s){
                if(mm.get("introduce").toString().equals(mm1.get("introduce").toString())){
                    if(!mm1.get("yesOrNo").equals(false)){
                        number++;
                    }

                }
            }
            mm.put("number",number);
            mm.put("introduce",mm.get("introduce"));
        }

        list1p.stream().sorted((a,b)->Integer.valueOf(a.get("number").toString())-Integer.valueOf(b.get("number").toString())).collect(Collectors.toList()).forEach(s -> System.out.println(s));

        return list1p;
    }
    /**
     * 考试程序端-考试答卷提交接口
     *   交卷接口
     * @Description: 考生提交考卷；参数：考生id，试卷id，答卷集合
     * @Param: [stuSignId, epaperId, answerList]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-27
     */
    @PostMapping("/selAnswerSheet")
    @ApiOperation(value = "考试程序端-考试答卷提交接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<?> selAnswerSheet(String stuSignId, String epaperId, String answerList, String code,Integer time,String batchId,String mobile,Integer examType,Integer isSubmit) {

        if (StringUtils.isBlank(stuSignId)) {
            return ApiResult.fail("");//缺少参数
        }
        if (StringUtils.isBlank(epaperId)) {
            return ApiResult.fail("");//缺少参数
        }
        ExaminationBatchDO examinationBatchDO=    examinationBatchService.getById(batchId);


        if(examType==2){
        DictDO dictDO=    dictService.getById(1591980861552926721L);
            if (isSubmit == 0) { //为0不强制提交/为1强制提交
                boolean b = DateTimeUtil.dateBefore1(examinationBatchDO.getStartTime(), Integer.valueOf(dictDO.getValue()));
                if (!b) {
                    return ApiResult.fail("还未达到交卷时间，暂时无法交卷！");//缺少参数
                }
            }
        }

        StuSignUpDO stuSignUpDO = stuSignUpService.getById(Long.parseLong(stuSignId));
        if (stuSignUpDO != null) {
            //添加认证码，认证码格式是： md5（"考号"+"YYMMDD"）
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(currentTime);
            boolean bb = MD5Util.verify(stuSignUpDO.getTicketNumber(), dateString, code);
            if (bb) {
                QueryWrapper<ExaminationAnswerSheetDO> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("epaper_id", epaperId);
                queryWrapper.eq("stu_sign_id", stuSignId);
                queryWrapper.eq("exam_type", examType);
                ExaminationAnswerSheetDO examinationAnswerSheetDO = examinationAnswerSheetService.getOne(queryWrapper );


                ExaminationAnswerSheetDO examinationAnswerSheetDO1 = new ExaminationAnswerSheetDO();
              if(examinationBatchDO!=null){
                  //获得考试结束时间
                  LocalDateTime   LocalEndTime=  LocalDateTimeUtil.dateToDateTime(  examinationBatchDO.getStartTime());
                  //获取交卷时间
                  LocalDateTime   localDateTime=    LocalDateTimeUtil.withPlus(0);
                  //计算时间相差间隔
                  String msgTime=   LocalDateTimeUtil.timeBetween(localDateTime,LocalEndTime);
                  examinationAnswerSheetDO1.setExaminationTime(msgTime);
              }else {
                  examinationAnswerSheetDO1.setExaminationTime("0小时60分钟0秒");
              }

                if (examinationAnswerSheetDO != null) {
                    return ApiResult.fail("试卷已提交，如有问题请联系监考员");
                } else {
                    //提交试卷新增绑定考试批次字段
                    examinationAnswerSheetDO1.setExamType(examType);
                    examinationAnswerSheetDO1.setEpaperId(Long.parseLong(epaperId));//试卷id
                    examinationAnswerSheetDO1.setStuSignId(Long.parseLong(stuSignId));//考生id
                    examinationAnswerSheetDO1.setAnswerList(answerList);//答卷集合
                    examinationAnswerSheetDO1.setCreate_time(LocalDateTime.now());
                    examinationAnswerSheetDO1.setTime(time);
                    examinationAnswerSheetDO1.setProName(examinationBatchDO.getName());
                    examinationAnswerSheetDO1.setUserid(JWTUtil.getUser().getId());
                    boolean b = examinationAnswerSheetService.save(examinationAnswerSheetDO1);
                    if (b) {
                        stuSignUpDO.setState(10);
                        boolean bbc = stuSignUpService.updateById(stuSignUpDO);
                        if (bbc) {
                            return ApiResult.success("提交成功",examinationAnswerSheetDO1.getId());
                        } else {
                            return ApiResult.fail("提交失败，请重试");
                        }
                    } else {
                        return ApiResult.fail("提交失败，请重试！");
                    }
                }
            } else {
                //saveLog("考试程序端-考试答卷提交接口", stuSignId + ";" + epaperId + ";" + answerList, stuSignUpDO.getUserId(), stuSignUpDO.getName());
                return ApiResult.fail("信息错误，请重试");
            }
        } else {
           // saveLog("考试程序端-考试答卷提交接口", stuSignId + ";" + epaperId + ";" + answerList, -2L, "不明来历者");
            return ApiResult.fail("信息错误，请重试");
        }
    }
}
